package net.gianrishandy.jbase.connection;

import java.sql.ResultSet;

/**
 * @author Gian Rishandy
 *
 */
public class SQLExecution {
	
	/**
	 * To execute SQL script 
	 * 
	 * @param  query (String of SQL script)
	 * @return ResultSet
	 * @throws Exception
	 */
	public static ResultSet executeQuery(String query) throws Exception{
		return Connection.databaseConnection.createStatement().executeQuery(query);
	}
	
	/**
	 * To execute update SQL script
	 * 
	 * @param  query (String of SQL script)
	 * @return boolean (true = success and false = failed)
	 * @throws Exception
	 */
	public static boolean executeUpdate(String query) throws Exception{ 
		int update = Connection.databaseConnection.createStatement().executeUpdate(query);
		switch(update){
			case 0 : return false;
			default: return true;
		}
	}
	
	/**
	 * Change SQL script to Command
	 * 
	 * @param query (String of SQL script)
	 * @return Command
	 */
	public static Command makeCommandQuery(String query){
		Command command = new Command();
		command.setQuery(query);
		return command;
	}
	
	/**
	 * Change of store procedure to Command
	 * 
	 * @param storeProcedure (Name of store procedure)
	 * @return Command
	 */
	public static Command makeCommandSP(String storeProcedure){
		Command command = new Command();
		command.setStoreProcedure(storeProcedure);
		return command;
	}
	
	/**
	 * Change of store procedure with parameter values to Command
	 * 
	 * @param storeProcedure (Name of store procedure)
	 * @param parameterValues (Array objects of parameter value)
	 * @return Command
	 */
	public static Command makeCommandSP(String storeProcedure, Object[] parameterValues){
		Command command = new Command();
		command.setStoreProcedure(storeProcedure);
		command.setParameterValue(parameterValues);
		return command;
	}
	
	/**
	 * Change of store procedure with parameter name and values to Command
	 * 
	 * @param storeProcedure (Name of store procedure)
	 * @param parameterNames (Array strings of parameter name)
	 * @param parameterValues (Array objects of parameter value)
	 * @return Command
	 */
	public static Command makeCommandSP(String storeProcedure, String[] parameterNames, Object[] parameterValues){
		Command command = new Command();
		command.setStoreProcedure(storeProcedure);
		command.setParameterName(parameterNames);
		command.setParameterValue(parameterValues);
		return command;
	}
}
